﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text.RegularExpressions;
    using UnityEditorInternal;
    using UnityEngine;

    internal class ProjectWindowUtil
    {
        [CompilerGenerated]
        private static Dictionary<string, int> <>f__switch$map15;
        internal static string k_DraggingFavoriteGenericData = "DraggingFavorite";
        internal static int k_FavoritesStartInstanceID = 0x3b9aca00;
        internal static string k_IsFolderGenericData = "IsFolder";
        internal static ProjectWindow s_PreferredProjectWindow;

        private static void CreateAnimatorController()
        {
            Texture2D image = EditorGUIUtility.IconContent("AnimatorController Icon").image as Texture2D;
            StartNameEditingIfProjectWindowExists(0, EndAction.DoCreateAnimatorController, "New Animator Controller.controller", image, null);
        }

        public static void CreateAsset(UnityEngine.Object asset, string pathName)
        {
            StartNameEditingIfProjectWindowExists(asset.GetInstanceID(), EndAction.SaveNewAsset, pathName, AssetPreview.GetMiniThumbnail(asset), null);
        }

        public static void CreateFolder()
        {
            StartNameEditingIfProjectWindowExists(0, EndAction.DoCreateFolder, "New Folder", EditorGUIUtility.IconContent(EditorResourcesUtility.emptyFolderIconName).image as Texture2D, null);
        }

        public static void CreatePrefab()
        {
            StartNameEditingIfProjectWindowExists(0, EndAction.DoCreatePrefab, "New Prefab.prefab", EditorGUIUtility.IconContent("Prefab Icon").image as Texture2D, null);
        }

        private static void CreateScriptAsset(string templatePath, string destName)
        {
            Texture2D icon = null;
            string extension = Path.GetExtension(destName);
            if (extension != null)
            {
                int num;
                if (<>f__switch$map15 == null)
                {
                    Dictionary<string, int> dictionary = new Dictionary<string, int>(4);
                    dictionary.Add(".js", 0);
                    dictionary.Add(".cs", 1);
                    dictionary.Add(".boo", 2);
                    dictionary.Add(".shader", 3);
                    <>f__switch$map15 = dictionary;
                }
                if (<>f__switch$map15.TryGetValue(extension, out num))
                {
                    switch (num)
                    {
                        case 0:
                            icon = EditorGUIUtility.IconContent("js Script Icon").image as Texture2D;
                            goto Label_0105;

                        case 1:
                            icon = EditorGUIUtility.IconContent("cs Script Icon").image as Texture2D;
                            goto Label_0105;

                        case 2:
                            icon = EditorGUIUtility.IconContent("boo Script Icon").image as Texture2D;
                            goto Label_0105;

                        case 3:
                            icon = EditorGUIUtility.IconContent("Shader Icon").image as Texture2D;
                            goto Label_0105;
                    }
                }
            }
            icon = EditorGUIUtility.IconContent("TextAsset Icon").image as Texture2D;
        Label_0105:
            StartNameEditingIfProjectWindowExists(0, EndAction.DoCreateScriptAsset, destName, icon, templatePath);
        }

        internal static UnityEngine.Object CreateScriptAssetFromTemplate(string pathName, string resourceFile)
        {
            string fullPath = Path.GetFullPath(pathName);
            StreamReader reader = new StreamReader(resourceFile);
            string input = reader.ReadToEnd();
            reader.Close();
            string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(pathName);
            input = Regex.Replace(Regex.Replace(input, "#NAME#", fileNameWithoutExtension), "#SCRIPTNAME#", Regex.Replace(fileNameWithoutExtension, " ", string.Empty));
            StreamWriter writer = new StreamWriter(fullPath);
            writer.Write(input);
            writer.Close();
            AssetDatabase.ImportAsset(pathName);
            return AssetDatabase.LoadAssetAtPath(pathName, typeof(UnityEngine.Object));
        }

        private static void DoCreateAnimatorController(int instanceID, string pathName)
        {
            ShowCreatedAsset(AnimatorController.CreateAnimatorControllerAtPath(pathName));
        }

        private static void DoCreateFolder(int instanceID, string pathName)
        {
            ShowCreatedAsset(AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(AssetDatabase.CreateFolder(Path.GetDirectoryName(pathName), Path.GetFileName(pathName))), typeof(UnityEngine.Object)));
        }

        private static void DoCreatePrefab(int instanceID, string pathName)
        {
            ShowCreatedAsset(PrefabUtility.CreateEmptyPrefab(pathName));
        }

        private static void DoCreateScriptAsset(int instanceID, string pathName, string resourceFile)
        {
            ShowCreatedAsset(CreateScriptAssetFromTemplate(pathName, resourceFile));
        }

        internal static void EndNameEditAction(EndAction action, int instanceId, string pathName, string resourceFile)
        {
            pathName = AssetDatabase.GenerateUniqueAssetPath(pathName);
            switch (action)
            {
                case EndAction.None:
                    Debug.LogError("WTF? Ending a none action");
                    break;

                case EndAction.SaveNewAsset:
                    SaveNewAsset(instanceId, pathName);
                    break;

                case EndAction.DoCreateFolder:
                    DoCreateFolder(instanceId, pathName);
                    break;

                case EndAction.DoCreatePrefab:
                    DoCreatePrefab(instanceId, pathName);
                    break;

                case EndAction.DoCreateScriptAsset:
                    DoCreateScriptAsset(instanceId, pathName, resourceFile);
                    break;

                case EndAction.DoCreateAnimatorController:
                    DoCreateAnimatorController(instanceId, pathName);
                    break;
            }
        }

        private static void FrameObjectInProjectWindow(int instanceID)
        {
            ProjectWindow projectWindowIfExists = GetProjectWindowIfExists();
            ObjectBrowser objectBrowserIfExists = GetObjectBrowserIfExists();
            if ((objectBrowserIfExists != null) && (s_PreferredProjectWindow == null))
            {
                objectBrowserIfExists.FrameObject(instanceID, false);
            }
            else if (projectWindowIfExists != null)
            {
                projectWindowIfExists.FrameObject(instanceID);
            }
        }

        public static string[] GetBaseFolders(string[] folders)
        {
            if (folders.Length < 2)
            {
                return folders;
            }
            List<string> list = new List<string>();
            List<string> list2 = new List<string>(folders);
            list2.Sort();
            string item = list2[0];
            list.Add(item);
            for (int i = 1; i < list2.Count; i++)
            {
                if (list2[i].IndexOf(item) < 0)
                {
                    list.Add(list2[i]);
                    item = list2[i];
                }
            }
            return list.ToArray();
        }

        internal static UnityEngine.Object[] GetDragAndDropObjects(int draggedInstanceID, List<int> selectedInstanceIDs)
        {
            if (selectedInstanceIDs.Contains(draggedInstanceID))
            {
                UnityEngine.Object[] objArray = new UnityEngine.Object[selectedInstanceIDs.Count];
                for (int i = 0; i < selectedInstanceIDs.Count; i++)
                {
                    objArray[i] = InternalEditorUtility.GetObjectFromInstanceID(selectedInstanceIDs[i]);
                }
                return objArray;
            }
            return new UnityEngine.Object[] { InternalEditorUtility.GetObjectFromInstanceID(draggedInstanceID) };
        }

        internal static string[] GetDragAndDropPaths(int draggedInstanceID, List<int> selectedInstanceIDs)
        {
            List<string> list = new List<string>();
            foreach (int num in selectedInstanceIDs)
            {
                if (AssetDatabase.IsMainAsset(num))
                {
                    string item = AssetDatabase.GetAssetPath(num);
                    list.Add(item);
                }
            }
            string assetPath = AssetDatabase.GetAssetPath(draggedInstanceID);
            if (string.IsNullOrEmpty(assetPath))
            {
                return new string[0];
            }
            if (list.Contains(assetPath))
            {
                return list.ToArray();
            }
            return new string[] { assetPath };
        }

        private static ObjectBrowser GetObjectBrowserIfExists()
        {
            return ObjectBrowser.s_LastInteractedObjectBrowser;
        }

        private static ProjectWindow GetProjectWindowIfExists()
        {
            if (s_PreferredProjectWindow != null)
            {
                return s_PreferredProjectWindow;
            }
            UnityEngine.Object[] objArray = Resources.FindObjectsOfTypeAll(typeof(ProjectWindow));
            if ((objArray != null) && (objArray.Length > 0))
            {
                return (objArray[0] as ProjectWindow);
            }
            return null;
        }

        internal static bool IsFavoritesItem(int instanceID)
        {
            return (instanceID >= k_FavoritesStartInstanceID);
        }

        private static void SaveNewAsset(int instanceID, string pathName)
        {
            AssetDatabase.CreateAsset(EditorUtility.InstanceIDToObject(instanceID), AssetDatabase.GenerateUniqueAssetPath(pathName));
            FrameObjectInProjectWindow(instanceID);
        }

        private static void ShowCreatedAsset(UnityEngine.Object o)
        {
            Selection.activeObject = o;
            if (o != null)
            {
                FrameObjectInProjectWindow(o.GetInstanceID());
            }
        }

        internal static void StartDrag(int draggedInstanceID, List<int> selectedInstanceIDs)
        {
            DragAndDrop.PrepareStartDrag();
            string title = string.Empty;
            if (IsFavoritesItem(draggedInstanceID))
            {
                DragAndDrop.SetGenericData(k_DraggingFavoriteGenericData, draggedInstanceID);
                DragAndDrop.objectReferences = new UnityEngine.Object[0];
            }
            else
            {
                bool isFolder = false;
                HierarchyProperty property = new HierarchyProperty(HierarchyType.Assets);
                if (property.Find(draggedInstanceID, null))
                {
                    isFolder = property.isFolder;
                }
                DragAndDrop.objectReferences = GetDragAndDropObjects(draggedInstanceID, selectedInstanceIDs);
                DragAndDrop.SetGenericData(k_IsFolderGenericData, !isFolder ? string.Empty : "isFolder");
                string[] dragAndDropPaths = GetDragAndDropPaths(draggedInstanceID, selectedInstanceIDs);
                if (dragAndDropPaths.Length > 0)
                {
                    DragAndDrop.paths = dragAndDropPaths;
                }
                if (DragAndDrop.objectReferences.Length > 1)
                {
                    title = "<Multiple>";
                }
                else
                {
                    title = ObjectNames.GetDragAndDropTitle(InternalEditorUtility.GetObjectFromInstanceID(draggedInstanceID));
                }
            }
            DragAndDrop.StartDrag(title);
        }

        private static void StartNameEditingIfProjectWindowExists(int instanceID, EndAction endAction, string pathName, Texture2D icon, string resourceFile)
        {
            ProjectWindow projectWindowIfExists = GetProjectWindowIfExists();
            ObjectBrowser objectBrowserIfExists = GetObjectBrowserIfExists();
            if ((objectBrowserIfExists != null) && (s_PreferredProjectWindow == null))
            {
                objectBrowserIfExists.Focus();
                objectBrowserIfExists.BeginPreimportedNameEditing(instanceID, endAction, pathName, icon, resourceFile);
                objectBrowserIfExists.Repaint();
            }
            else if (projectWindowIfExists != null)
            {
                projectWindowIfExists.Focus();
                projectWindowIfExists.BeginPreimportedNameEditing(instanceID, endAction, pathName, icon, resourceFile);
                projectWindowIfExists.Repaint();
            }
            else
            {
                if (!pathName.StartsWith("assets/", StringComparison.CurrentCultureIgnoreCase))
                {
                    pathName = "Assets/" + pathName;
                }
                EndNameEditAction(endAction, instanceID, pathName, resourceFile);
                Selection.activeObject = EditorUtility.InstanceIDToObject(instanceID);
            }
        }

        internal enum EndAction
        {
            None,
            SaveNewAsset,
            DoCreateFolder,
            DoCreatePrefab,
            DoCreateScriptAsset,
            DoCreateAnimatorController
        }
    }
}

